package com.lt.demo.controller;

import com.lt.demo.domain.Image;
import com.lt.demo.domain.User;
import com.lt.demo.service.ImageService;
import com.lt.demo.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/image")
public class ImageController {

    @Autowired
    private ImageService imageService;

    @Autowired
    private UserService userService;

    @PostMapping(value = "/convert", consumes = "multipart/form-data")
    @Operation(summary = "用户上传图片，设定参数，进行转化", description = "AI生成图片")
    @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "成功生成")
    @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "300", description = "内部异常")
    public ApiResponse<Image> uploadImage(@RequestParam("file") MultipartFile file, @RequestParam("style") String style,
                                          @RequestParam("size") Integer size, @RequestParam("containOriginalColor") Boolean containOriginalColor) {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userService.findByUsername(userDetails.getUsername());
        try {
            Image image = imageService.storeImage(file, user, style, size, containOriginalColor);
            return new ApiResponse<>(200, "Image uploaded successfully", image);
        } catch (IOException e) {
            e.printStackTrace();
            return new ApiResponse<>(300, "Failed to upload image", null);
        }
    }

    @PostMapping(value = "/fake/convert",consumes = "multipart/form-data")
    @Operation(summary = "用户上传图片，设定参数，进行转化", description = "直接返回图片")
    @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "成功生成")
    @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "300", description = "内部异常")
    public ApiResponse<Image> uploadFakeImage(@RequestParam("file") MultipartFile file, @RequestParam("style") String style,
                                          @RequestParam("size") Integer size, @RequestParam("containOriginalColor") Boolean containOriginalColor) {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userService.findByUsername(userDetails.getUsername());
        try {
            Image image = imageService.storeFakeImage(file, user, style, size, containOriginalColor);
            return new ApiResponse<>(200, "Image uploaded successfully", image);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse<>(300, "Failed to upload image", null);
        }
    }

    @GetMapping("/list")
    @Operation(summary = "用户获取已经转化的图片", description = "用户获取已经转化的图片")
    @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "成功获取")
    @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "300", description = "内部异常")
    public ApiResponse<List<Image>> getImages() {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userService.findByUsername(userDetails.getUsername());
        List<Image> images = imageService.getImagesByUser(user);
        return new ApiResponse<>(200, "Images fetched successfully", images);
    }
}